package com.lxl.view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.lxl.model.BookUser;
import com.lxl.service.BookUserService;
import com.lxl.service.impl.BookUserServiceImpl;

import java.awt.Font;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * @author 刘星鹭
 * @Email liuxinglumail@163.com
 * @version 创建时间： 2014年9月12日 上午10:00:31
 */

public class LoginFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField bookUserName;
	private JTextField bookUserPassword;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginFrame frame = new LoginFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public LoginFrame() {
		addWindowListener(new WindowAdapter() {
		});
		setTitle("图书管理系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 441, 261);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel = new JPanel();
		panel.setBounds(5, 168, 415, 55);
		contentPane.add(panel);

		JButton btnNewButton = new JButton("注册");
		btnNewButton.setBounds(137, 10, 63, 25);
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				String username = bookUserName.getText();
				String password = bookUserPassword.getText();
				if (username.equals("") || password.equals(""))
					JOptionPane.showMessageDialog(null, "请填写注册资料！");
				else {
					BookUserService bookUserService = new BookUserServiceImpl();
					bookUserService.persist(new BookUser(username, password));
					JOptionPane.showMessageDialog(null, "注册成功！");
				}
			}
		});
		panel.setLayout(null);
		btnNewButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		panel.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("登陆");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnNewButton_1.setBounds(212, 10, 63, 25);
		btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				String username = bookUserName.getText();
				String password = bookUserPassword.getText();
				if (username.equals("") || password.equals(""))
					JOptionPane.showMessageDialog(null, "请填写登陆资料！");
				else if (username.equals("system") && password.equals("dbsnmp")) {
					ManagerView.main(new String[1]);
					dispose();
				} else
					JOptionPane.showMessageDialog(null,
							"目前仅限管理员登陆，请检查您的管理员账户和密码！");
			}
		});
		btnNewButton_1
				.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		panel.add(btnNewButton_1);

		JPanel panel_1 = new JPanel();
		panel_1.setBounds(5, 5, 415, 164);
		panel_1.setToolTipText("啊");
		contentPane.add(panel_1);
		panel_1.setLayout(null);

		bookUserName = new JTextField();
		bookUserName.setName("");
		bookUserName.setBounds(146, 77, 154, 21);
		panel_1.add(bookUserName);
		bookUserName.setColumns(10);

		JLabel label = new JLabel("欢迎使用图书管理系统");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label.setBounds(111, 10, 180, 47);
		panel_1.add(label);

		JLabel label_1 = new JLabel("用户名：");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_1.setBounds(100, 80, 54, 18);
		panel_1.add(label_1);

		JLabel label_2 = new JLabel("密  码：");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_2.setBounds(100, 122, 54, 18);
		panel_1.add(label_2);

		bookUserPassword = new JPasswordField();
		bookUserPassword.setName("");
		bookUserPassword.setColumns(10);
		bookUserPassword.setBounds(146, 121, 154, 21);
		panel_1.add(bookUserPassword);
	}
}
